<?php
/**
 * User: b179a9
 * Date: 06.09.11
 * Time: 19:49
 */

class Lib_Flash
{
    const ERROR = 'error';
    const WARNING = 'warning';
    const NOTICE = 'notice';

    const POST = 'flash-post';
    const PRE = 'flash-pre';

    const NOW = self::POST;
    const LATER = self::PRE;

    /**
     * @param $message
     * @param string $class
     * @param string $type
     * @return Lib_Flash
     */
    public function addMessage($message, $class = self::ERROR, $type = self::POST)
    {
        $session = new Zend_Session_Namespace($type);
        $messages = $session->messages;
        if(!is_array($messages)) {
            $messages = array();
        }
        if(!array_key_exists($class, $messages)) {
            $messages[$class] = array();
        }
        array_push($messages[$class], $message);
        $session->messages = $messages;
        return $this;
    }

    /**
     * @param string $type
     * @return void
     */
    public function clearMessages($type = self::POST)
    {
        $session = new Zend_Session_Namespace($type);
        $session->unsetAll();
    }

    /**
     * @param string $type
     * @return array
     */
    public function getMessages($type = self::POST)
    {
        $session = new Zend_Session_Namespace($type);
        $messages = $session->messages;
        if(!is_array($messages)) {
            return array();
        }
        return $messages;
    }

}
